#pragma once

// -----------------------------------------------------------------------------

#include <atlbase.h>
#include <strsafe.h>

namespace Kif
{
namespace Win32
{
namespace IO
{

/// <summary>
/// Provide support to manage the IStream interface that is used in XmlLite.
///
/// From the MSDN...
/// The IStream interface lets you read and write data to stream objects. Stream
/// objects contain the data in a structured storage object, where storages
/// provide the structure. Simple data can be written directly to a stream but,
/// most frequently, streams are elements nested within a storage object. They
/// are similar to standard files.
///
/// The IStream interface defines methods similar to the MS-DOS FAT file
/// functions. For example, each stream object has its own access rights and a
/// seek pointer. The main difference between a DOS file and a stream object is
/// that in the latter case, streams are opened using an IStream interface
/// pointer rather than a file handle.
///
/// The methods in this interface present your object's data as a contiguous
/// sequence of bytes that you can read or write. There are also methods for
/// committing and reverting changes on streams that are open in transacted mode
/// and methods for restricting access to a range of bytes in the stream.
///
/// Streams can remain open for long periods of time without consuming
/// file-system resources. The IUnknown::Release method is similar to a close
/// function on a file. Once released, the stream object is no longer valid and
/// cannot be used.
/// </summary>

class StreamTextReader
{
   private:
   HRESULT lastHResult;
   CComPtr<IStream> pFileStream;

   public:

   StreamTextReader()
   {
      lastHResult = 0;
      pFileStream = NULL;
   }  // end of TextReader (ctor)

   inline HRESULT GetLastHResult()
   {
      return lastHResult;
   }  // end of GetLastHResult

   inline CComPtr<IStream> GetFileStream()
   {
      return pFileStream;
   }  // end of GetFileStream

   StreamTextReader *OpenTextReader(TCHAR *filePath);

   inline static StreamTextReader *CreateTextReader(TCHAR *filePath)
   {
      StreamTextReader *pReader = new StreamTextReader();
      // TODO: put an exception here to trap the no pReader allocation
      if (pReader == NULL)
         return NULL;
      return pReader->OpenTextReader(filePath);
   }  // end of CreateTextReader

}  ;

}  // end of Kif::Win32::IO
}  // end of Kif::Win32
}  // end of Kif

